
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>串的表示和实现 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="串的模式匹配算法.html" />
    
    
    <link rel="prev" href="串类型.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    数据结构（C语言版） 复习总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1.html">
            
                <a href="../chapter1.html">
            
                    
                    第一章 绪论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/chapter2.html">
            
                <a href="../chapter2/chapter2.html">
            
                    
                    第二章 线性表
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/顺序表.html">
            
                <a href="../chapter2/顺序表.html">
            
                    
                    线性表的顺序表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/链表.html">
            
                <a href="../chapter2/链表.html">
            
                    
                    线性表的链式表示和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/chapter3.html">
            
                <a href="../chapter3/chapter3.html">
            
                    
                    第三章 栈和队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/栈.html">
            
                <a href="../chapter3/栈.html">
            
                    
                    抽象数据类型栈的定义和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/队列.html">
            
                <a href="../chapter3/队列.html">
            
                    
                    抽象数据类型队列的定义和实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    第四章 串
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="串类型.html">
            
                <a href="串类型.html">
            
                    
                    串类型的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="串的表示和实现.html">
            
                <a href="串的表示和实现.html">
            
                    
                    串的表示和实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="串的模式匹配算法.html">
            
                <a href="串的模式匹配算法.html">
            
                    
                    串的模式匹配算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    第五章 数组和广义表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/数组的定义.html">
            
                <a href="../chapter5/数组的定义.html">
            
                    
                    数组的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/矩阵的压缩存储.html">
            
                <a href="../chapter5/矩阵的压缩存储.html">
            
                    
                    矩阵的压缩存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter5/广义表.html">
            
                <a href="../chapter5/广义表.html">
            
                    
                    广义表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    第六章 树和二叉树
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter6/树.html">
            
                <a href="../chapter6/树.html">
            
                    
                    树的定义与基本术语
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/二叉树.html">
            
                <a href="../chapter6/二叉树.html">
            
                    
                    二叉树
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.2.1" data-path="../chapter6/二叉树的二叉链表实现.html">
            
                <a href="../chapter6/二叉树的二叉链表实现.html">
            
                    
                    二叉树的二叉链表实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter6/遍历二叉树和线索二叉树.html">
            
                <a href="../chapter6/遍历二叉树和线索二叉树.html">
            
                    
                    遍历二叉树和线索二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter6/树和森林.html">
            
                <a href="../chapter6/树和森林.html">
            
                    
                    树和森林
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter6/赫夫曼树.html">
            
                <a href="../chapter6/赫夫曼树.html">
            
                    
                    赫夫曼树
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    第七章 图
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter7/图.html">
            
                <a href="../chapter7/图.html">
            
                    
                    图的定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter7/图的存储结构.html">
            
                <a href="../chapter7/图的存储结构.html">
            
                    
                    图的存储结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter7/图的遍历.html">
            
                <a href="../chapter7/图的遍历.html">
            
                    
                    图的遍历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter7/最小生成树.html">
            
                <a href="../chapter7/最小生成树.html">
            
                    
                    最小生成树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter7/有向无环图.html">
            
                <a href="../chapter7/有向无环图.html">
            
                    
                    有向无环图的应用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter8/查找.html">
            
                <a href="../chapter8/查找.html">
            
                    
                    第八章 查找
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter8/静态查找表.html">
            
                <a href="../chapter8/静态查找表.html">
            
                    
                    静态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter8/动态查找表.html">
            
                <a href="../chapter8/动态查找表.html">
            
                    
                    动态查找表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter8/哈希表.html">
            
                <a href="../chapter8/哈希表.html">
            
                    
                    哈希表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter9/内部排序.html">
            
                <a href="../chapter9/内部排序.html">
            
                    
                    第九章 内部排序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter9/插入排序.html">
            
                <a href="../chapter9/插入排序.html">
            
                    
                    插入排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter9/选择排序.html">
            
                <a href="../chapter9/选择排序.html">
            
                    
                    选择排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter9/快速排序.html">
            
                <a href="../chapter9/快速排序.html">
            
                    
                    快速排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter9/归并排序和基数排序.html">
            
                <a href="../chapter9/归并排序和基数排序.html">
            
                    
                    归并排序和基数排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter9/排序方法的比较.html">
            
                <a href="../chapter9/排序方法的比较.html">
            
                    
                    排序方法的比较
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >串的表示和实现</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x4E32;&#x7684;&#x8868;&#x793A;&#x548C;&#x5B9E;&#x73B0;">&#x4E32;&#x7684;&#x8868;&#x793A;&#x548C;&#x5B9E;&#x73B0;</h2>
<h3 id="&#x5B9A;&#x957F;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x8868;&#x793A;">&#x5B9A;&#x957F;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x8868;&#x793A;</h3>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x7EBF;&#x6027;&#x8868;&#x7684;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#xFF0C;&#x7528;&#x4E00;&#x7EC4;&#x5730;&#x5740;&#x8FDE;&#x7EED;&#x7684;&#x5B58;&#x50A8;&#x5355;&#x5143;&#x5B58;&#x50A8;&#x4E32;&#x503C;&#x7684;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#x3002;&#x4E3A;&#x6BCF;&#x4E2A;&#x5B9A;&#x4E49;&#x7684;&#x4E32;&#x53D8;&#x91CF;&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x56FA;&#x5B9A;&#x957F;&#x5EA6;&#x7684;&#x5B58;&#x50A8;&#x533A;&#xFF0C;&#x7528;&#x5B9A;&#x957F;&#x6570;&#x7EC4;&#x63CF;&#x8FF0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-C"><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAXSTRLEN 30 <span class="hljs-comment">// &#x7528;&#x6237;&#x53EF;&#x5728; 30 &#x4EE5;&#x5185;&#x5B9A;&#x4E49;&#x6700;&#x5927;&#x4E32;&#x957F;</span></span>

<span class="hljs-keyword">typedef</span> <span class="hljs-keyword">char</span> SString[Maxstrlen + <span class="hljs-number">1</span>]; <span class="hljs-comment">// 0 &#x53F7;&#x5355;&#x5143;&#x5B58;&#x653E;&#x4E32;&#x7684;&#x957F;&#x5EA6;</span>
</code></pre>
<blockquote>
<p>&#x4E32;&#x7684;&#x5B9E;&#x9645;&#x957F;&#x5EA6;&#x53EF;&#x5728;&#x8FD9;&#x9884;&#x5B9A;&#x4E49;&#x7684;&#x957F;&#x5EA6;&#x8303;&#x56F4;&#x5185;&#xFF0C;&#x8D85;&#x8FC7;&#x9884;&#x5B9A;&#x4E49;&#x957F;&#x5EA6;&#x7684;&#x90E8;&#x5206;&#x4F1A;&#x88AB;&#x820D;&#x53BB;&#xFF0C;&#x79F0;&#x4E4B;&#x4E3A;<strong>&#x622A;&#x65AD;</strong>&#x3002;</p>
</blockquote>
<p>&#x5B9A;&#x957F;&#x987A;&#x5E8F;&#x4E32;&#x64CD;&#x4F5C;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<ol>
<li><p>&#x4E32;&#x8054;&#x63A5; <code>concat(&amp;T, S1, S2)</code></p>
<p> &#x4E32; T &#x662F;&#x7531;&#x4E32; S1 &#x8054;&#x7ED3;&#x4E32; S2 &#x5F97;&#x5230;&#x7684;&#xFF0C;&#x53EA;&#x9700;&#x8FDB;&#x884C;&#x76F8;&#x5E94;&#x7684;&#x201C;&#x4E32;&#x503C;&#x590D;&#x5236;&#x201D;&#x64CD;&#x4F5C;&#x5373;&#x53EF;&#xFF0C;&#x5BF9;&#x8D85;&#x957F;&#x90E8;&#x5206;&#x8FDB;&#x884C;&#x622A;&#x65AD;&#x3002;</p>
<pre><code class="lang-C"> <span class="hljs-comment">/**
  * &#x4E32; S1 &#x8054;&#x7ED3;&#x4E32; S2
  * &#x8054;&#x63A5;&#x6210;&#x529F;&#x8FD4;&#x56DE; TRUE&#xFF0C;&#x6709;&#x622A;&#x65AD;&#x8FD4;&#x56DE; FALSE 
  */</span>
 <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">concat</span><span class="hljs-params">(SString &amp;T, SString S1, SString S2)</span>
 </span>{
     <span class="hljs-keyword">bool</span> flag = FLASE;
     <span class="hljs-keyword">if</span> (S1[<span class="hljs-number">0</span>] + S2[<span class="hljs-number">0</span>] &lt;= MAXSTRLEN) <span class="hljs-comment">// &#x65E0;&#x9700;&#x622A;&#x65AD;</span>
     {
         T[<span class="hljs-number">1.</span>.S1[<span class="hljs-number">0</span>]] = S1[<span class="hljs-number">1.</span>.S1[<span class="hljs-number">0</span>]];
         T[S1[<span class="hljs-number">0</span>] + <span class="hljs-number">1.</span>.S1[<span class="hljs-number">0</span>] + S2[<span class="hljs-number">0</span>]] = S2[<span class="hljs-number">1.</span>.S2[<span class="hljs-number">0</span>]];
         T[<span class="hljs-number">0</span>] = S1[<span class="hljs-number">0</span>] + S2[<span class="hljs-number">0</span>];
         flag = TRUE;
     }
     <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (S1[<span class="hljs-number">0</span>] &lt; MAXSTRLEN) <span class="hljs-comment">// S1 &#x4E0E; S2 &#x957F;&#x5EA6;&#x4E4B;&#x548C;&#x5927;&#x4E8E; MAXSTRLEN&#xFF0C;&#x9700;&#x8981;&#x622A;&#x65AD;</span>
     {
         T[<span class="hljs-number">1.</span>.S1[<span class="hljs-number">0</span>]] = S1[<span class="hljs-number">1.</span>.S1[<span class="hljs-number">0</span>]];
         T[S1[<span class="hljs-number">0</span>] + <span class="hljs-number">1.</span>.MAXSTRLEN = S2[<span class="hljs-number">1.</span>.MAXSTRLEN - S1[<span class="hljs-number">0</span>]];
         T[<span class="hljs-number">0</span>] = MAXSTRLEN;
     }
     <span class="hljs-keyword">else</span> <span class="hljs-comment">// S1 &#x7684;&#x957F;&#x5EA6;&#x7B49;&#x4E8E; MAXSTRLEN&#xFF0C;&#x9700;&#x8981;&#x622A;&#x65AD;</span>
     {
         T[<span class="hljs-number">0.</span>.MAXSTRLEN] = S1[<span class="hljs-number">0.</span>.MAXSTRLEN];
     }
     <span class="hljs-keyword">return</span> flag;
 }
</code></pre>
</li>
<li><p>&#x6C42;&#x5B50;&#x4E32; <code>subString(S, pos, len)</code></p>
<pre><code class="lang-C"> <span class="hljs-comment">/**
  * &#x83B7;&#x53D6;&#x4E32; S &#x4ECE;&#x7B2C; pos &#x4E2A;&#x5B57;&#x7B26;&#x5F00;&#x59CB;&#x957F;&#x5EA6;&#x4E3A; len &#x7684;&#x5B50;&#x4E32;
  */</span>
 <span class="hljs-function">SString <span class="hljs-title">subString</span><span class="hljs-params">(SString S, <span class="hljs-keyword">int</span> pos, <span class="hljs-keyword">int</span> len)</span>
 </span>{
     SString sub;
     <span class="hljs-keyword">if</span> (pos &lt; <span class="hljs-number">1</span> || pos &gt;= S[<span class="hljs-number">0</span>] || len &lt; <span class="hljs-number">0</span> || len &gt; S[<span class="hljs-number">0</span>]-pos+<span class="hljs-number">1</span>)
     {
         <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
     }
     Sub[<span class="hljs-number">1.</span>.len] = S[pos..pos + len - <span class="hljs-number">1</span>]
     Sub[<span class="hljs-number">0</span>] = len;
     <span class="hljs-keyword">return</span> Sub;
 }
</code></pre>
</li>
</ol>
<h3 id="&#x5806;&#x5206;&#x914D;&#x5B58;&#x50A8;&#x8868;&#x793A;">&#x5806;&#x5206;&#x914D;&#x5B58;&#x50A8;&#x8868;&#x793A;</h3>
<p>&#x4ECD;&#x4EE5;&#x4E00;&#x7EC4;&#x5730;&#x5740;&#x8FDE;&#x7EED;&#x7684;&#x5B58;&#x50A8;&#x5355;&#x5143;&#x5B58;&#x50A8;&#x4E32;&#x503C;&#x5B57;&#x7B26;&#x5E8F;&#x5217;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x662F;&#x5728;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x52A8;&#x6001;&#x5206;&#x914D;&#x800C;&#x5F97;&#xFF0C;&#x56E0;&#x6B64;&#x64CD;&#x4F5C;&#x4E2D;&#x5BF9;&#x4E8E;&#x4E32;&#x957F;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x9650;&#x5236;&#x3002;</p>
<blockquote>
<p>&#x5728; C &#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x79F0;&#x4E4B;&#x4E3A;&#x201C;&#x5806;&#x201D;&#x7684;&#x81EA;&#x7531;&#x5B58;&#x50A8;&#x533A;&#xFF0C;&#x5E76;&#x7531; C &#x8BED;&#x8A00;&#x7684;&#x52A8;&#x6001;&#x5206;&#x914D;&#x51FD;&#x6570; malloc() &#x548C; free() &#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;</p>
<ul>
<li>&#x5229;&#x7528;&#x51FD;&#x6570; malloc() &#x4E3A;&#x6BCF;&#x4E2A;&#x65B0;&#x4EA7;&#x751F;&#x7684;&#x4E32;&#x5206;&#x914D;&#x4E32;&#x957F;&#x6240;&#x9700;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#xFF0C;&#x5206;&#x914D;&#x6210;&#x529F;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6307;&#x5411;&#x8D77;&#x59CB;&#x5730;&#x5740;&#x7684;&#x6307;&#x9488;&#xFF0C;&#x4F5C;&#x4E3A;&#x4E32;&#x7684;&#x57FA;&#x5740;&#xFF0C;&#x7EA6;&#x5B9A;&#x4E32;&#x957F;&#x4E5F;&#x662F;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x90E8;&#x5206;</li>
</ul>
</blockquote>
<pre><code class="lang-C">/**
 * &#x4E32;&#x7684;&#x5806;&#x5206;&#x914D;&#x5B58;&#x50A8;&#x7ED3;&#x6784;
 */
typedef struct
{
    char *ch; // &#x7A7A;&#x4E32;&#x4EE4; ch &#x4E3A; NULL
    int length; // &#x4E32;&#x957F;&#x5EA6;
}HString;

/**
 * &#x751F;&#x6210;&#x4E00;&#x4E2A;&#x4E32;&#x503C;&#x7B49;&#x4E8E; chars &#x7684;&#x4E32; T
 * &#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x4EE5;&#x5728;&#x4E32;&#x503C;&#x540E;&#x9762;&#x52A0; &apos;\0&apos; &#x8868;&#x793A;&#x4E32;&#x503C;&#x7684;&#x7EC8;&#x7ED3;
 */
bool strAssign(HString &amp;T, char *chars)
{
    int i;
    char *c;
    if (T.ch)
    {
        free ( T.ch ); // &#x9996;&#x5148;&#x91CA;&#x653E; T &#x5360;&#x7528;&#x7684;&#x7A7A;&#x95F4;
    }
    for (i = 0, c = chars; *c; ++i, ++c); // &#x8BA1;&#x7B97;chars &#x7684;&#x957F;&#x5EA6;
    if (i == 0) // &#x82E5; i == 0&#xFF0C;&#x5219;&#x7F6E; T &#x4E3A;&#x7A7A;&#x4E32; T
    {
        T.ch = NULL;
        T.length = 0;
    }
    else // &#x82E5; i != 0&#xFF0C;&#x5219;&#x5C06; chars &#x5185;&#x5BB9;&#x590D;&#x5236;&#x7ED9; T
    {                            
        if (!(T.ch = (char *) malloc (i * sizeof(char)))) // &#x7A7A;&#x95F4;&#x5206;&#x914D;&#x5931;&#x8D25;
        {
            printf(&quot;error\n&quot;);
            exit(0);
        }
        T.ch[0..i-1] = chars[0..i-1];
        T.length = i;
    }
    return OK;
}

/**
 * &#x4E32;&#x4E4B;&#x95F4;&#x7684;&#x6BD4;&#x8F83;
 * &#x8FD4;&#x56DE;&#x503C;&#x5927;&#x4E8E; 0&#xFF1A;S &gt; T
 * &#x8FD4;&#x56DE;&#x503C;&#x7B49;&#x4E8E; 0&#xFF1A;S = T
 * &#x8FD4;&#x56DE;&#x503C;&#x5C0F;&#x4E8E; 0&#xFF1A;S &lt; T
 */
int strCompare(HString S, HString T)
{
    int i;
    for (i = 0; i &lt; S.length &amp;&amp; i &lt; T.length; i++)
    {
        if (S.ch[i] != T.ch[i]) // &#x9010;&#x4E2A;&#x5B57;&#x7B26;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;
        {
            return S.ch[i] - T.ch[i];
        }
    }
    return S.length - T.length; // &#x6240;&#x6709;&#x5B57;&#x7B26;&#x90FD;&#x4E00;&#x6837;&#xFF0C;&#x5219;&#x6839;&#x636E;&#x4E32;&#x957F;&#x77ED;&#x51B3;&#x5B9A;&#x5927;&#x5C0F;
}

/**
 * &#x5C06;&#x4E32; S &#x7F6E;&#x4E3A;&#x7A7A;&#x4E32;
 */
void clearString(HString &amp;S)
{
    if (S.ch)
    {
        free(S.ch);
        S.ch = NULL;
    }
    S.length = 0;
}
</code></pre>
<blockquote>
<p>&#x5176;&#x4ED6;&#x64CD;&#x4F5C;&#x4F8B;&#x5982;&#x8054;&#x63A5;&#x3001;&#x53D6;&#x5B50;&#x4E32;&#x7B49;&#xFF0C;&#x4E0E;&#x5B9A;&#x957F;&#x987A;&#x5E8F;&#x5B58;&#x50A8;&#x76F8;&#x4F3C;&#xFF0C;&#x53EA;&#x662F;&#x9700;&#x8981;&#x5728;&#x4F7F;&#x7528;&#x4E32;&#x524D;&#x4E3A;&#x5176;&#x5206;&#x914D;&#x7A7A;&#x95F4;&#x3002;</p>
</blockquote>
<h3 id="&#x4E32;&#x7684;&#x5757;&#x94FE;&#x5B58;&#x50A8;&#x8868;&#x793A;">&#x4E32;&#x7684;&#x5757;&#x94FE;&#x5B58;&#x50A8;&#x8868;&#x793A;</h3>
<p>&#x5F53;&#x4F7F;&#x7528;&#x94FE;&#x8868;&#x5B58;&#x50A8;&#x4E32;&#x503C;&#x65F6;&#xFF0C;&#x6BCF;&#x4E2A;&#x7ED3;&#x70B9;&#x4E2D;&#x53EF;&#x4EE5;&#x5B58;&#x653E;&#x591A;&#x4E2A;&#x5B57;&#x7B26;&#x3002;&#x5F53;&#x7ED3;&#x70B9;&#x5927;&#x5C0F;&#x5927;&#x4E8E; 1 &#x65F6;&#xFF0C;&#x7531;&#x4E8E;&#x4E32;&#x957F;&#x4E0D;&#x4E00;&#x5B9A;&#x662F;&#x7ED3;&#x70B9;&#x5927;&#x5C0F;&#x7684;&#x6574;&#x6570;&#x500D;&#xFF0C;&#x5219;&#x94FE;&#x8868;&#x4E2D;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;&#x4E0D;&#x4E00;&#x5B9A;&#x5168;&#x88AB;&#x4E32;&#x503C;&#x5360;&#x6EE1;&#xFF0C;&#x6B64;&#x65F6;&#x901A;&#x5E38;&#x8865;&#x4E0A; &quot;#&quot;&#x3002;&#x9664;&#x4E86;&#x5934;&#x6307;&#x9488;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x9644;&#x8BBE;&#x4E00;&#x4E2A;&#x5C3E;&#x6307;&#x9488;&#xFF0C;&#x6307;&#x5411;&#x94FE;&#x8868;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;&#xFF0C;&#x5E76;&#x7ED9;&#x51FA;&#x5F53;&#x524D;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x5C06;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#x7684;&#x4E32;&#x5B58;&#x50A8;&#x7ED3;&#x6784;&#x79F0;&#x4E3A;<strong>&#x5757;&#x94FE;&#x7ED3;&#x6784;</strong>&#x3002;</p>
<pre><code>#define CHUNKSIZE 10 // &#x5B9A;&#x4E49;&#x5757;&#x7684;&#x5927;&#x5C0F;

/**
 * &#x5B9A;&#x4E49;&#x5757;&#x7ED3;&#x6784;
 */
typedef struct Chunk
{
    char ch[CHUNKSIZE];
    struct Chunk *next;
}Chunk;

/**
 * &#x5B9A;&#x4E49;&#x5757;&#x94FE;&#x5B58;&#x50A8;&#x8868;&#x793A;
 */
typedef struct
{
    Chunk *head, *tail;
    int length;
}
</code></pre><blockquote>
<p>&#x8BBE;&#x7F6E;&#x5C3E;&#x6307;&#x9488;&#x7684;&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x4FBF;&#x4E8E;&#x8FDB;&#x884C;&#x8054;&#x63A5;&#x64CD;&#x4F5C;&#xFF0C;&#x8054;&#x63A5;&#x65F6;&#x9700;&#x8981;&#x5904;&#x7406;&#x4E32;&#x5C3E;&#x7684;&#x65E0;&#x6548;&#x5B57;&#x7B26;&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="串类型.html" class="navigation navigation-prev " aria-label="Previous page: 串类型的定义">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="串的模式匹配算法.html" class="navigation navigation-next " aria-label="Next page: 串的模式匹配算法">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"串的表示和实现","level":"1.5.2","depth":2,"next":{"title":"串的模式匹配算法","level":"1.5.3","depth":2,"path":"chapter4/串的模式匹配算法.md","ref":"chapter4/串的模式匹配算法.md","articles":[]},"previous":{"title":"串类型的定义","level":"1.5.1","depth":2,"path":"chapter4/串类型.md","ref":"chapter4/串类型.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter4/串的表示和实现.md","mtime":"2018-08-16T10:03:37.394Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-02T10:05:33.540Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

